package com.hailong.zyt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hailong.zyt.beans.Course;
import com.hailong.zyt.beans.Student;
import com.hailong.zyt.beans.Teacher;
import com.hailong.zyt.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hailong
 * @since 2021-05-13
 */
@Slf4j
@Controller
public class CourseController {

    private CourseService courseService;

    @Autowired
    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }


    //来到教师课程页面
    @GetMapping("/teacher/course")
    public String toTeacherCoursePage(HttpSession session, Model model) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        wrapper.eq("tea_id", teacher.getTeaId());
        List<Course> list = courseService.list(wrapper);
        model.addAttribute("courses", list);
        return "/teacher/course/teaCourse";
    }

    //添加课程
    @GetMapping("/teacher/addCourse")
    public String toTeacherAddCourse(HttpSession session, Model model) {
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        model.addAttribute("teacher", teacher);
        return "/teacher/course/addCourse";
    }
    @PostMapping("/teacher/addCourse")
    public String teacherAddCourse(Course newCourse) {
        courseService.save(newCourse);
        return "redirect:/teacher/course";
    }

    /**
     * 成绩统计
     * */
    @GetMapping("/teacher/showGradeList/{couId}")
    public String toGradeList(@PathVariable("couId") Long couId, HttpSession session, Model model){
        Teacher teacher = (Teacher) session.getAttribute("teacher");
        courseService.getGradeList(model, couId, teacher.getTeaId());
        return "/teacher/course/gradeList";
    }

    /**------------------------------------------------------------------------------------------------------------*/

    @GetMapping("/student/addCourse")
    public String studentAddCourse(HttpSession session, Model model) throws ParseException {
        Student student = (Student) session.getAttribute("student");
        List<Course> courses = courseService.getOptionalCourses(student.getStuId());
        model.addAttribute("courses", courses);
        return "/student/course/addCourse";
    }
}

